package com.bee.mydiary.record;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class RecordServer {
		private Context mcontext;
		private String ID = "id";
		private String NAME = "name";
		private String PATH = "path";

		public RecordServer(Context context){
			mcontext = context;
		}

		public int getNum() {
			int num = 0;
			Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
			ContentResolver mContentResolver = mcontext.getContentResolver();

			Cursor cursor = mContentResolver.query(uri, null, null,
					null, null);
			if (cursor != null && cursor.getCount() != 0) {
				num = cursor.getCount();
			}
			return num;
		}

		public List<HashMap<String, String>> getRecord() {
//			Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
			Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
			ContentResolver mContentResolver = mcontext.getContentResolver();
//			String selection = MediaStore.Images.Media.MIME_TYPE + "=?";
			String[] projection = { 
			MediaStore.Audio.Media._ID,
			MediaStore.Audio.Media.DISPLAY_NAME,
			MediaStore.Audio.Media.DATA,
			MediaStore.Audio.Media.SIZE
			};
//			String[] selectionArgs = { "image/jpeg" };
			String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc";

			List<HashMap<String, String>> records = new ArrayList<HashMap<String, String>>();
			Cursor cursor = mContentResolver.query(uri, projection, null,
					null, sortOrder);
			if (cursor != null) {

				cursor.moveToFirst();
				while (cursor.moveToNext()) {
					HashMap<String, String> record = new HashMap<String, String>();
					record.put(ID, cursor.getString(cursor
							.getColumnIndex(MediaStore.Audio.Media._ID)));
					record.put(NAME, cursor.getString(cursor
							.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
					record.put(PATH, cursor.getString(cursor
							.getColumnIndex(MediaStore.Audio.Media.DATA)));
					
					records.add(record);
				}
				cursor.close();
			}

			return records;
		}

	}
